package com.jt.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jt.mapper.MemberCardMapper;
import com.jt.pojo.MemberCard;
import com.jt.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class MemberCardServiceImpl implements MemberCardService{

    @Autowired
    private MemberCardMapper memberCardMapper;
    @Override
    public PageResult getMemberCardList(PageResult pageResult) {
        QueryWrapper<MemberCard> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.hasLength(pageResult.getQuery()),"name",pageResult.getQuery());
        IPage<MemberCard> page = new Page<>(pageResult.getPageNum(),pageResult.getPageSize());
        page = memberCardMapper.selectPage(page,queryWrapper);
        long total = page.getTotal();
        List<MemberCard> rows = page.getRecords();
        return pageResult.setTotal(total).setRows(rows);
    }

    @Override
    @Transactional
    public void addMemberCard(MemberCard memberCard) {
        memberCardMapper.insert(memberCard);
    }

    @Override
    public MemberCard getMemberCard(Integer id) {
        return memberCardMapper.selectById(id);
    }

    @Override
    public void updatedMemberCard(MemberCard memberCard) {
        memberCardMapper.updateById(memberCard);
    }

    @Override
    @Transactional
    public void deleteMemberCard(Integer id) {
        memberCardMapper.deleteById(id);
    }
}
